
package org.wk.upms.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wk.core.common.view.ResponseVo;
import org.wk.logger.common.annotation.SysLog;
import org.wk.upms.common.constant.Constant;
import org.wk.upms.service.DeptService;
import org.wk.upms.view.DeptVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	@Autowired
	private DeptService sysDeptService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dept:list")
	public List<DeptVo> list(){
		List<DeptVo> deptList = sysDeptService.queryList(new HashMap<String, Object>());
		return deptList;
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:dept:select")
	public ResponseVo select(){
		List<DeptVo> deptList = sysDeptService.queryList(new HashMap<String, Object>());
		//添加一级部门
		if(getUserId() == Constant.SUPER_ADMIN){
            DeptVo root = new DeptVo();
			root.setName("一级部门");
			root.setParentId(null);
			root.setOpen(true);
			deptList.add(root);
		}
		return ResponseVo.success().setData(deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:dept:list")
	public ResponseVo info(){
		String deptId = "";

		//如果不是管理员
		if(!Constant.SUPER_ADMIN.equals(getUserId())){
			List<DeptVo> deptList = sysDeptService.queryList(new HashMap<String, Object>());
			String parentId = null;
			for(DeptVo sysDeptEntity : deptList){
				if(parentId == null){
					parentId = sysDeptEntity.getParentId();
					continue;
				}

				if(parentId.equals(sysDeptEntity.getParentId())){
					parentId = sysDeptEntity.getParentId();
				}
			}
			deptId = parentId;
		}

        Map<String,String> resData = new HashMap<>();
		resData.put("deptId",deptId);
		return ResponseVo.success().setData(resData);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("sys:dept:info")
	public ResponseVo info(@PathVariable("deptId") String deptId){
		DeptVo dept = sysDeptService.selectById(deptId);

//        Map<String,Object> resData = new HashMap<>();
//        resData.put(dept);
        return ResponseVo.success().setData(dept);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存部门")
	@RequestMapping("/save")
	@RequiresPermissions("sys:dept:save")
	public ResponseVo save(@RequestBody DeptVo dept){
		sysDeptService.insert(dept);
		
		return ResponseVo.success();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改部门信息")
	@RequestMapping("/update")
	@RequiresPermissions("sys:dept:update")
	public ResponseVo update(@RequestBody DeptVo dept){
		sysDeptService.updateById(dept);
		
		return ResponseVo.success();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除部门信息")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dept:delete")
	public ResponseVo delete(String deptId){
		//判断是否有子部门
		List<String> deptList = sysDeptService.queryDeptIdList(deptId);
		if(null!=deptList && deptList.size() > 0){
			return ResponseVo.error("请先删除子部门");
		}

		sysDeptService.deleteById(deptId);
		
		return ResponseVo.success();
	}
	
}
